<body>

    <table class="layui-hide" id="cardTableId" lay-filter="caedTable"></table>
    <!-- 按钮 -->
    <div style="display: none" id="userbarDemo" type="text/html">
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    </div>

    <script>
        layui.use(['jquery', 'layer', 'table', 'form', 'config'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var form = layui.form;
            var config = layui.config;
            form.render();
            // 表格渲染
            table.render({
                elem: '#cardTableId',
                id: 'card'
                , url: config.base_server + 'getCardList'
                , cellMinWidth: 80
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'id', title: '订单编号', sort: true, minWidth: 100 }
                    , { field: 'username', title: '用户名称', minWidth: 100 }
                    , { field: 'sum', title: '商品价格', minWidth: 100 }
                    , {
                        field: 'comm', title: '商品价格', minWidth: 100,
                        templet: function (d) {
                            // d.url.map(item=>{})
                            let str = d.comm.reduce(function (prev, cur, index, arr) {
                                if (index == 0) {
                                    return cur.name + ":" + cur.number
                                }
                                return prev + "--" + cur.name + ":" + cur.number
                            }, '')
                            return str
                        }
                    }
                    , { title: '操作', toolbar: '#userbarDemo' }
                ]]
                , page: true //开启分页
                , skin: 'row' //行边框风格
                , even: true //开启隔行背景
                , size: 'lg' //小尺寸的表格
            });

            //行内按钮事件的监听
            table.on('tool(caedTable)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    var editId = data.id;
                    layer.confirm('真的删除该订单吗?', function (index) {
                        $.ajax({
                            type: "GET",
                            url: config.base_server + "DeleteCardById",
                            data: { id: editId },
                            dataType: "json",
                            success: (data) => {
                                if (data.code) {
                                    obj.del();
                                    // table.reload('card');
                                    layer.msg('删除成功', {
                                        icon: 1
                                    });
                                    layer.close(index);
                                } else {
                                    alert(data.info);
                                }
                            }
                        });
                    });
                }
            });
        });

    </script>

</body>